package com.yugao.fintech.libra.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yugao.fintech.libra.common.constants.HttpCons;
import com.yugao.fintech.libra.common.ram.ActionType;
import com.yugao.fintech.libra.common.ram.RamResource;
import com.yugao.fintech.libra.system.model.rqrs.tenant.*;
import com.yugao.fintech.libra.system.service.TenantPackageService;
import com.yugao.fintech.meheco.core.domain.ResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/tenantPackage")
@Tag(name = "租户套餐")
public class TenantPackageController {
    @Autowired
    private TenantPackageService tenantPackageService;

    @PostMapping("/save")
    @Operation(summary = "创建租户套餐")
    @RamResource(action = "sys:tenantPackage:create", type = ActionType.WRITE, name = "创建租户套餐")
    public ResultVO<Long> saveTenantPackage(@Valid @RequestBody TenantPackageSaveReq req) {
        return ResultVO.success(tenantPackageService.saveTenantPackage(req));
    }

    @PostMapping("/update")
    @Operation(summary = "更新租户套餐")
    @RamResource(action = "sys:tenantPackage:update", type = ActionType.WRITE, name = "更新租户套餐")
    public ResultVO<Boolean> updateTenantPackage(@Valid @RequestBody TenantPackageUpdateReq req) {
        tenantPackageService.updateTenantPackage(req);
        return ResultVO.success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除租户套餐")
    @Parameter(name = "id", description = "编号", required = true)
    @RamResource(action = "sys:tenantPackage:delete", type = ActionType.WRITE, name = "删除租户套餐")
    public ResultVO<Boolean> deleteTenantPackage(@RequestParam("id") Long id) {
        tenantPackageService.deleteTenantPackage(id);
        return ResultVO.success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得租户套餐")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @RamResource(action = "sys:tenantPackage:query", type = ActionType.READ, name = "查询租户套餐")
    public ResultVO<TenantPackageResp> getTenantPackage(@RequestParam("id") Long id) {
        TenantPackageResp tenantPackage = tenantPackageService.getTenantPackage(id);
        return ResultVO.success(tenantPackage);
    }

    @GetMapping("/page")
    @Operation(summary = "获得租户套餐分页")
    @RamResource(action = "sys:tenantPackage:query", type = ActionType.READ, name = "查询租户套餐")
    public ResultVO<IPage<TenantPackageResp>> getTenantPackagePage(@Valid TenantPackageBasePageReq req) {
        IPage<TenantPackageResp> page = tenantPackageService.pageTenantPackage(req);
        return ResultVO.success(page);
    }

    @GetMapping("/list")
    @RamResource(action = "sys:tenantPackage:query", type = ActionType.READ, name = "查询租户套餐")
    @Operation(summary = "获取租户套餐精简信息列表", description = "只包含被开启的租户套餐，主要用于前端的下拉选项")
    public ResultVO<List<TenantPackageResp>> listTenantPackage(@Validated TenantPackageListReq req) {
        // 获得角色列表，只要开启状态的
        req.setStatus(HttpCons.NORMAL);
        List<TenantPackageResp> list = tenantPackageService.listTenantPackage(req);
        return ResultVO.success(list);
    }
}
